<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.searchKeyWord"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="可按设备类型、编号、id查找"
      />
      <el-select v-model="listQuery.room" class="filter-item" placeholder="全部机房" @change="getList" clearable>
        <el-option
          v-for="item in roomList"
          :key="item.id"
          :label="item.name"
          :value="item.name"/>
      </el-select>
      <el-button v-permission="['computer:room:list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['computer:room:create']" class="filter-item" type="primary" icon="el-icon-s-grid" @click="stateChange('')">全部</el-button>
      <el-button v-permission="['computer:room:create']" class="filter-item" type="primary" icon="el-icon-warning" @click="stateChange('1')">未处理</el-button>
      <el-button v-permission="['computer:room:create']" class="filter-item" type="primary" icon="el-icon-success" @click="stateChange('2')">已处理</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column type="index" align="center" label="序号" width="50">
        <template scope="scope">
          <span v-if="scope.$index < 9">0{{ scope.$index + 1 }}</span>
          <span v-else>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="设备id" prop="equipmentId" /> -->
      <el-table-column align="center" label="所属机房" prop="roomName"/>
      <el-table-column align="center" label="设备类型" prop="equipmentType" />
      <el-table-column align="center" label="设备编号" prop="equipmentNo"/>
      <el-table-column align="center" label="异常事项" prop="abnormalMatters"/>
      <el-table-column align="center" label="附件" prop="show">
        <template slot-scope="scope">
          <a style="color:#5f7de9" @click="enlarge(scope.row)">查看</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="abnormalStatus">
        <template slot-scope="scope">
          {{ scope.row.abnormalStatus == 6?'未处理':'已处理' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width" min-width="140px">
        <template slot-scope="scope">
          <el-button v-permission="['computer:room:edit']" type="primary" size="small" @click="remarks(scope.row)">备注</el-button>
          <el-button v-permission="['computer:room:delete']" v-if="scope.row.abnormalStatus == 6" type="danger" size="small" @click="handle(scope.row)">处理</el-button>
          <!-- <el-button v-permission="['computer:room:edit']" v-if="scope.row.showFlag == 0" type="danger" size="small" @click="showChange(scope.row)">默认展示</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="id" prop="id" hidden>
          <el-input v-model="dataForm.id" />
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="dataForm.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :loading="submiting" type="primary" @click="createData">确定</el-button>
        <el-button v-else :loading="submiting" type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
    <!-- 备注 -->
    <el-dialog :visible.sync="remarksVisible" :before-close="remarksClose" title="备注" width="30%">
      <el-input v-model="value" :rows="4" type="textarea" maxlength="1000" show-word-limit/>
      <div class="submit_css">
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </el-dialog>
    <!-- 查看附件 -->
    <el-dialog :visible.sync="dialogVisible" :before-close="handleClose" title="提示">
      <el-carousel :interval="5000" arrow="always" style="width:100%" height="500px">
        <el-carousel-item v-for="item in srcList" :key="item">
          <div style="margin:0 auto">
            <img :src="item" alt="" style="width:100%;height:500px">
          </div>
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  createComputerRoom,
  updateComputerRoom,
  deleteComputerRoom,
  showSwitch,
  xiaLaList,
  remarks,
  handle
} from '@/api/abnormalHandle'
import Pagination from '@/components/Pagination'
export default {
  name: 'ComputerRoom',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        searchType: '',
        searchKeyWord: '',
        room: ''
      },
      dataForm: {
        id: undefined
      },
      dialogFormVisible: false,
      submiting: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        name: [{ required: true, message: 'name名称不能为空', trigger: 'blur' }]
      },
      roomList: [],
      remarksVisible: false,
      value: '',
      remarksInfo: {},
      // 查看附件
      dialogVisible: false,
      srcList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    enlarge(val) {
      this.dialogVisible = true
      // val.photo = "https://taohaoliang.oss-cn-beijing.aliyuncs.com/admin/195c8e3d3035404bb8cdbe176949076c.jpg,https://taohaoliang.oss-cn-beijing.aliyuncs.com/admin/1d9d101513914135b19295ce68b1ed17.jpg"
      this.srcList = val.photo.split(',')
    },
    handleClose() {
      this.srcList = []
      this.dialogVisible = false
    },
    getList() {
      this.listLoading = true
      xiaLaList(this.listQuery).then(response => { // 机房列表
        if (response.data.data.length > 0) {
          this.roomList = response.data.data
        }
      }).catch(() => { })
      getList(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    submit() {
      this.$confirm('确定该设备备注？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remarks({ id: this.remarksInfo.id, remarks: this.value }).then(response => {
          this.remarksVisible = false
          this.$notify.success({
            title: '成功',
            message: '提交成功'
          })
        }).catch(() => {})
      })
    },
    stateChange(num) {
      this.listQuery.searchType = num
      this.getList()
    },
    remarks(row) { // 备注
      this.remarksInfo = row
      this.remarksVisible = true
    },
    remarksClose() {
      this.remarksVisible = false
    },
    handle(val) { // 处理
      this.$confirm('确定该设备已恢复正常', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        handle({ id: val.id }).then(response => {
          //  this.remarksVisible = false
          this.$notify.success({
            title: '成功',
            message: '处理成功'
          })
          this.getList()
        }).catch(() => {})
      })
    },
    showChange(val) { // 隐藏、显示
      showSwitch({ id: val.id }).then(response => {
        this.$notify.success({
          title: '成功',
          message: '默认展示成功'
        })
        this.getList()
      })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submiting = true
          createComputerRoom(this.dataForm).then(response => {
            this.list.unshift(response.data.data)
            this.dialogFormVisible = false
            this.$notify.success({
              title: '成功',
              message: '添加成功'
            })
            this.submiting = false
          })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
              this.submiting = false
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submiting = true
          updateComputerRoom(this.dataForm).then(() => {
            for (const v of this.list) {
              if (v.id === this.dataForm.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.dataForm)
                break
              }
            }
            this.dialogFormVisible = false
            this.submiting = false
            this.$notify.success({
              title: '成功',
              message: '更新成功'
            })
          })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
              this.submiting = false
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该记录---' + row.name + '---, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteComputerRoom(row.id).then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch(() => {
        return false
      })
    }
  }
}
</script>
<style>
.submit_css{
    margin: 30px auto 0;
    text-align: center;
}

</style>
